//
//  StarInfoViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/14.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StarInfoViewController.h"
#import "StarView.h"
#import "StarInfoModel.h"
#import "LeftModel.h"
#import "LeftModelCell.h"
#import "SearchListController.h"
#import "DetailViewController.h"
#define starInfo_url @"http://api.app.happyjuzi.com/foundpage/list/starinfo"

@interface StarInfoViewController ()

@property (strong, nonatomic) NSMutableArray   *dataSource_news;
@property (strong, nonatomic) NSMutableArray   *dataSource_wish;
@property (nonatomic, strong) StarView         *infoView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) StarInfoModel *model;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation StarInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //头部视图
    self.infoView  = [StarView infoView];
    //初始化数组
    self.dataSource_news = [NSMutableArray new];
    
    //创建尾部视图
    self.tableView.tableFooterView = [self createFooterView];
    

    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"LeftModelCell" bundle:nil] forCellReuseIdentifier:@"LeftModel"];
    self.tableView.rowHeight = 110;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self refreshData];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.tableView.backgroundColor = [UIColor blackColor];
    } else {//日间模式
//        self.tableView.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:0.7];
    }
    [self.tableView reloadData];
}

//添加刷新
- (void)refreshData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

//请求网络数据
- (void)loadDataFromServerWithPage:(NSInteger)page {
    NSDictionary *parmar = @{@"res":@"1152x1920",@"accesstoken":@"55ce1e68910a13aa8090590d5bb2a0de",@"sid":@(self.sid),@"pf":@"android",@"uid":@"3971130771637759",@"page":@(page),@"ts":@0,@"channel":@"yingyongbao",@"net":@"wifi",@"mac":@"38-bc-1a-25-a0-0f",@"ver":@"2.11"};
    [[AFHTTPSessionManager manager]GET:starInfo_url parameters:parmar progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (page == 1) {
            [self.dataSource_wish removeAllObjects];
            [self.dataSource_news removeAllObjects];
        }
        
        NSDictionary *dataDict = responseObject[@"data"];
        NSDictionary *star = dataDict[@"star"];
        StarInfoModel *model = [StarInfoModel new];
        [model setValuesForKeysWithDictionary:star];
        self.infoView.model = model;
        self.model = model;
        self.infoView.frame = CGRectMake(0, 0, ScreenSizeW, model.height);
        self.tableView.tableHeaderView = self.infoView;
        
        [self parserSearchResultWithDict:responseObject];//解析news
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

//解析搜索结果
- (void)parserSearchResultWithDict:(NSDictionary *)responseObject {
    NSDictionary *dataDict = responseObject[@"data"];
    for (NSDictionary *dict in dataDict[@"news"]) {
        LeftModel *model = [LeftModel new];
        [model setValuesForKeysWithDictionary:dict];
        [self.dataSource_news addObject:model];
    }
}

//获取新数据
- (void)loadNewData {
    self.page = 1;
    [self loadDataFromServerWithPage:self.page];
}
//加载更多数据
//- (void)loadMoreData {
//    self.page++;
//    [self loadDataFromServerWithPage:self.page];
//}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.dataSource_news.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftModelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftModel"];
    cell.model = self.dataSource_news[indexPath.row];
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [cell updateToNightSkinMode];
    }else {
        [cell updateToDaySkinMode];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    LeftModel *model = self.dataSource_news[indexPath.row];
    detailVC.ID = model.ID;
    detailVC.listmodel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

//创建尾部视图
- (UIView *)createFooterView {
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
    footerView .backgroundColor = [UIColor clearColor];
    UIButton *footerBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    footerBtn.frame = CGRectMake(40, 10, ScreenSizeW - 80, 30);
    [footerBtn setTitle:@"查看更多热门文章" forState:(UIControlStateNormal)];
    [footerBtn addTarget:self action:@selector(footerBtnClickAction) forControlEvents:(UIControlEventTouchUpInside)];
    footerBtn.layer.cornerRadius = 10;
    footerBtn.layer.masksToBounds = YES;
    footerBtn.backgroundColor = [UIColor orangeColor];
    [footerView addSubview:footerBtn];
    return footerView;
}

- (void)footerBtnClickAction {
    SearchListController *searchListVC = [[SearchListController alloc]init];
    searchListVC.keyword = self.model.name;
    [self.navigationController pushViewController:searchListVC animated:YES];
}

@end
